package mastermind.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Simple socket wrapper
 * @author Brendan
 *
 */
public class socketConnect {

	private ServerSocket serverSocket = null;
	private Socket serviceSocket = null;
	private DataInputStream input;
	private DataOutputStream output;
	
	/**
	 * Construct and listen.
	 */
	public socketConnect()
	{
		try 
		{
			if(serverSocket == null)
			{
				serverSocket = new ServerSocket(protocol.SERVER_PORT);
				System.out.println("Server: Waiting for a client...");
				serviceSocket = serverSocket.accept();
				System.out.println("Server: Listening on port " + protocol.SERVER_PORT);
			}

			input = new DataInputStream(serviceSocket.getInputStream());
			output = new DataOutputStream(serviceSocket.getOutputStream());
			
		} 
		catch (IOException e) 
		{
			System.out.println("Could not listen on port: " + protocol.SERVER_PORT);
			e.printStackTrace();
		}
	}
	
	/**
	 * Get server socket
	 * @return the server socket
	 */
	public ServerSocket getServerSocket()
	{
		return serverSocket;
	}
	
	/**
	 * Get input stream
	 * @return the input stream
	 */
	public DataInputStream getInputStream()
	{
		return input;
	}
	
	/**
	 * Get output stream
	 * @return the output stream
	 */
	public DataOutputStream getOutputStream()
	{
		return output;
	}
	
	/**
	 * Close socket and clean up.
	 */
	public void closeSockets()
	{
		try 
		{
			input.close();
			output.close();
			serviceSocket.close();
			serverSocket.close();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}

	}
	
	
	
}
